package com.joeyzh.iframe.db.entity;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.joey.base.util.ResourcesUtils;
import com.joeyzh.iframe.db.BaseDbEntity;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Transient;
import org.greenrobot.greendao.annotation.Generated;

/**
 * Created by Joey on 2018/10/30.
 *
 * @author Joey
 */
@Entity
public class MenuItemEntity extends BaseDbEntity {

    @Id
    private String Id;
    private String Name;
    private String Url;
    private String Icon;
    @Transient
    private int resId;
    @Transient
    private int bubble;
    @Transient
    private Intent intent;


    @Generated(hash = 60741715)
    public MenuItemEntity(String Id, String Name, String Url, String Icon) {
        this.Id = Id;
        this.Name = Name;
        this.Url = Url;
        this.Icon = Icon;
    }

    @Generated(hash = 1099527587)
    public MenuItemEntity() {
    }

    public int getResId() {
        return resId;
    }

    public void setResId(int resId) {
        this.resId = resId;
    }

    public int getBubble() {
        return bubble;
    }

    public void setBubble(int bubble) {
        this.bubble = bubble;
    }

    public Intent getIntent() {
        return intent;
    }

    public void setIntent(Intent intent) {
        this.intent = intent;
    }

    public String getId() {
        return this.Id;
    }

    public void setId(String Id) {
        this.Id = Id;
    }

    public String getName() {
        return this.Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public String getUrl() {
        return this.Url;
    }

    public void setUrl(String Url) {
        this.Url = Url;
    }

    public String getIcon() {
        return this.Icon;
    }

    public void setIcon(String Icon) {
        this.Icon = Icon;
    }

    public Intent createIntent() {
        // 表示没有配置跳转页面
        if (TextUtils.isEmpty(getUrl())) {
            return intent;
        }
        // 生成Intent
        intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(getUrl()));
        if (TextUtils.isEmpty(getIcon())) {
            String[] values = getUrl().split("/");
            if (values.length >= 1) {
                setIcon(values[values.length - 1]);
            } else {
                setIcon(values[0]);
            }
        }
        int resid = ResourcesUtils.getDrawableID("ic_menu_" + getIcon().toLowerCase());
        if (resid < 0) {
            setIcon("default");
            resid = ResourcesUtils.getDrawableID("ic_menu_" + getIcon().toLowerCase());
        }
        // 生成Res
        setResId(resid);
        return intent;
    }
}
